package com.funong.matrix.biz.massage.facade;

import com.funong.matrix.biz.base.entity.SysSendTaskEntity;
import com.funong.matrix.biz.massage.pojo.BusinessTemplateDTO;
import com.funong.matrix.biz.massage.pojo.SendMessageDTO;
import com.funong.matrix.core.enums.MessageSendStatusEnum;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

/**
 * @author houhaipeng
 * @version 1.0
 * @date 2021/3/18 13:42
 */
@Component
public class MessageSendStrategyContext {
    private final Map<String, IMessageSender> sendMessageStrategyMap = new ConcurrentHashMap<>();

    @Autowired
    public void strategyInterface(Map<String, IMessageSender> strategyMapInfo) {
        this.sendMessageStrategyMap.clear();
        strategyMapInfo.forEach((k, v) -> this.sendMessageStrategyMap.put(k, v));
    }

    private IMessageSender getStrategy(String messageType) {
        return messageType != null ? sendMessageStrategyMap.get(messageType) : null;
    }

    /*@Async*/
    public String sendMessage(SendMessageDTO sendMessageDTO, BusinessTemplateDTO businessTemplateDTO, SysSendTaskEntity sendTaskEntity) {
        if (null != sendMessageDTO.getMessageType()) {
            IMessageSender sender = this.getStrategy(sendMessageDTO.getMessageType());
            return sender.sendMessage(sendMessageDTO,businessTemplateDTO,sendTaskEntity);
        }
        return MessageSendStatusEnum.UNSEND.name();
    }

    /*@Async*/
    public String sendMessage(SysSendTaskEntity sendTaskEntity) {
        if (null != sendTaskEntity.getMessageType()) {
            IMessageSender sender = this.getStrategy(sendTaskEntity.getMessageType());
            return sender.sendMessage(sendTaskEntity);
        }
        return MessageSendStatusEnum.UNSEND.name();
    }
}
